package com.gobang.common.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 游戏房间ID生成器
 * 生成唯一的房间ID
 * 
 * @author System
 * @since 1.0.0
 */
public class GameRoomIdGenerator {

    /**
     * 序列号计数器
     */
    private static final AtomicLong SEQUENCE = new AtomicLong(0);

    /**
     * 房间ID前缀
     */
    private static final String ROOM_PREFIX = "room_";

    /**
     * 时间格式化器
     */
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");

    /**
     * 生成房间ID
     * 格式：room_yyyyMMdd_HHmmss_sequence
     *
     * @return 房间ID
     */
    public static String generateRoomId() {
        String timestamp = LocalDateTime.now().format(FORMATTER);
        long sequence = SEQUENCE.incrementAndGet() % 10000; // 保持在4位数内
        // 房间ID：room_时间戳_4位序列号
        return String.format("%s%s_%04d", ROOM_PREFIX, timestamp, sequence);
    }

    /**
     * 验证房间ID格式
     *
     * @param roomId 房间ID
     * @return 是否为有效格式
     */
    public static boolean isValidRoomId(String roomId) {
        if (roomId == null || roomId.trim().isEmpty()) {
            return false;
        }
        
        // 简单的格式验证：以room_开头，长度合理
        return roomId.startsWith(ROOM_PREFIX) && roomId.length() >= 20 && roomId.length() <= 50;
    }

    /**
     * 重置序列号（测试用）
     */
    public static void resetSequence() {
        SEQUENCE.set(0);
    }
} 